<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"

      xmlns:f="http://java.sun.com/jsf/core">
    <ui:composition template="layout.xhtml">
        <ui:define name="body">
            <h:form id="cepForm" enctype="multipart/form-data">
                <h1 class="title">Escrituras</h1>
                <p:growl id="messages" showDetail="true" autoUpdate="true"  /> 

                <div class="row">
                    <div class="twelve columns"> 
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Pesquisar</h4></legend>
                            <fieldset>
                                <legend> <h4 style="margin:0 0 10px 0">Escritura</h4></legend>
                                <p:panel id="charterPanel" style="background: none; border: none;" 
                                         styleClass="container_24 clearfix ">

                                    <p:outputLabel value="Número: " styleClass="grid_3"/>
                                    <p:inputText styleClass="grid_8" value="#{managerSearchCep.charter.esc}"/>

                                    <div class="clear"/>

                                    <p:outputLabel value="Tipo Ato: " styleClass="grid_3"/>
                                    <p:inputText styleClass="grid_7" value="#{managerSearchCep.actCep.description}"
                                                 readonly="true"/>
                                    <p:commandButton  icon="ui-icon-plus" process="@this" 
                                                      styleClass="ui-button-icon-only grid_2" 
                                                      immediate="true"
                                                      update="@this, :cepForm:actCepPanelSearch"
                                                      style="width: 25px; height: 28px; margin-left:0px;" 
                                                      onclick="dlgActCepDialog.show();"/>

                                    <p:autoComplete rendered="false" styleClass="grid_8" style="width: 28.44%" dropdown="true" converter="actcepconverter"   
                                                    var="actCepValue" itemLabel="#{actCepValue.description}" scrollHeight="200" forceSelection="true"
                                                    itemValue="#{actCepValue}" value="#{managerSearchCep.charter.actCep}"
                                                    completeMethod="#{managerActCep.autocomplete}" />  

                                    <p:outputLabel value="Natureza: " styleClass="grid_4"/>
                                    <p:inputText styleClass="grid_7" value="#{managerSearchCep.natureWritingAux.description}"
                                                 readonly="true"/>
                                    <p:commandButton  icon="ui-icon-plus" process="@this" 
                                                      styleClass="ui-button-icon-only grid_2" 
                                                      immediate="true"
                                                      update="@this, :cepForm:naturePanelSearch"
                                                      style="width: 25px; height: 28px; margin-left:0px;" 
                                                      onclick="dlgNatureDialog.show();"/>


                                    <p:autoComplete rendered="false" styleClass="grid_8" style="width: 28.44%" dropdown="true"
                                                    converter="naturewritingconverter"   value="#{managerSearchCep.charter.natureWriting}"
                                                    var="natureWritingValue" itemLabel="#{natureWritingValue.description}" 
                                                    itemValue="#{natureWritingValue}" scrollHeight="200"
                                                    completeMethod="#{managerNatureWriting.autocomplete}" /> 

                                    <div class="clear"/>

                                    <p:outputLabel value="Data Ato: " styleClass="grid_3"/>
                                    <p:calendar showOn="button" styleClass="grid_8" style="width:33.1%"
                                                value="#{managerSearchCep.charter.dateAct}"/>

                                    <p:outputLabel value="Valor: " style="width:14%;" styleClass="grid_4"/>
                                    <p:inputText styleClass="grid_8" value="#{managerSearchCep.charter.currency}"/>

                                    <div class="clear"/>

                                    <p:outputLabel value="Livro: " styleClass="grid_3" />
                                    <p:inputMask mask="?999999999"  placeHolder=""  styleClass="grid_8"  value="#{managerSearchCep.charter.book}"/>

                                    <p:outputLabel value="Livro Compl.: " styleClass="grid_4"/>
                                    <p:inputText styleClass="grid_8" value="#{managerSearchCep.charter.bookComp}"/> 

                                    <div class="clear"/>

                                    <p:outputLabel value="Folha: " styleClass="grid_3" />
                                    <p:inputMask mask="?999999999"  placeHolder=""  styleClass="grid_8" value="#{managerSearchCep.charter.leaf}"/> 

                                    <p:outputLabel value="Folha Compl.: " styleClass="grid_4"/>
                                    <p:inputText styleClass="grid_8" value="#{managerSearchCep.charter.leafComp}"/> 


                                </p:panel>

                            </fieldset>
                            <fieldset>
                                <legend> <h4 style="margin:0 0 10px 0">Partes</h4></legend>

                                <p:panel id="partsPanel" style="background: none; border: none;"
                                         styleClass="container_24 clearfix ">

                                    <p:outputLabel value="Pessoa: "  styleClass="grid_3"/>
                                    <p:selectOneRadio id="typePerson" style="margin-top:-8px;" styleClass="grid_8"
                                                      value="#{managerSearchCep.people.tipoDocumento}" >  
                                        <f:selectItems value="#{managerCep.typeDocumentList()}" />
                                        <p:ajax event="change" update=":cepForm:partsPanel"  />
                                    </p:selectOneRadio>

                                    <div class="clear"/>

                                    <p:outputLabel id="typeDocumentLabel" 
                                                   value="#{managerSearchCep.people.tipoDocumento}: " 
                                                   styleClass="grid_3"/>

                                    <p:inputMask id="typeDocumentInput" 
                                                 value="#{managerSearchCep.people.tipoDocumento.type eq 'CPF' ? 
                                                          managerSearchCep.people.cpf : managerSearchCep.people.cnpj}"
                                                 mask="#{managerSearchCep.people.tipoDocumento.type eq 'CPF' ? 
                                                         '999.999.999-99':'99.999.999/9999-99'}"
                                                 styleClass="grid_8" /> 

                                    <div class="clear"/>

                                    <p:outputLabel value="Nome: " styleClass="grid_3" />
                                    <p:inputText styleClass="grid_20" value="#{managerSearchCep.people.name}"/> 

                                    <div class="clear"/>

                                    <p:outputLabel value="Qualidade: " 
                                                   styleClass="grid_3"/>
                                    <p:autoComplete styleClass="grid_8" style="width: 28.44%" dropdown="true" converter="qualityconverter"   
                                                    var="qualityValue" itemLabel="#{qualityValue.description}" scrollHeight="200"
                                                    forceSelection="true" itemValue="#{qualityValue}" 
                                                    value="#{managerSearchCep.part.partQuality}"
                                                    completeMethod="#{managerSearchCep.autocompleteQuality}" />
                                </p:panel>
                            </fieldset>
                            <fieldset>
                                <legend> <h4 style="margin:0 0 10px 0">Selos</h4></legend>

                                <p:panel id="stampPanel" 
                                         style="background: none; border: none;" styleClass="container_24 clearfix ">

                                    <p:outputLabel value="Série: "   styleClass="grid_3"/>
                                    <p:inputText  styleClass="grid_8" value="#{managerSearchCep.stamp.serie}"/> 

                                    <p:outputLabel value="Número: "  styleClass="grid_4"/>
                                    <p:inputText  styleClass="grid_8" value="#{managerSearchCep.stamp.number}"/> 

                                    <p:outputLabel value="Tipo Selo: "  styleClass="grid_3" />
                                    <p:autoComplete styleClass="grid_8" style="width: 28.44%" dropdown="true"
                                                    converter="typestampconverter"   
                                                    value="#{managerSearchCep.typeStampObj}"
                                                    var="typeStampValue" itemLabel="#{typeStampValue.name}" 
                                                    itemValue="#{typeStampValue}"
                                                    completeMethod="#{managerTypeStamp.autoComplete}" >

                                        <p:column style="width:20%;text-align:center;background-color: \##{typeStampValue.color};">  
                                        </p:column>  

                                        <p:column style="width:80%">  
                                            #{typeStampValue.name} 
                                        </p:column>
                                    </p:autoComplete>

                                </p:panel>
                            </fieldset>
                        </fieldset>
                        <div class="buttonAction">
                            <p:commandButton id="searchBtn" value="Pesquisar" actionListener="#{managerSearchCep.search()}" 
                                             icon="ui-icon-search" styleClass="ui-priority-primary"
                                             process="@form"
                                             update="@form, cepSearchTable" />
                            <p:commandButton value="Cancelar"  icon="ui-icon-trash" />
                        </div >

                        <h4 style="margin:0 0 10px 0">Resultado</h4>
                        <p:blockUI block="resultPanel" trigger="searchBtn" > 
                            <p:graphicImage value="media/css/images/ajax-loader.gif"/>
                        </p:blockUI>
                        <p:panel id="resultPanel" 
                                 style="background: none; border: none;" styleClass="container_24 clearfix ">

                            <div>
                                <p:outputLabel value="Filtro: "   styleClass="grid_2"/>
                                <p:selectOneMenu id="filter" value="#{managerSearchCep.filter}" >  
                                    <f:selectItem itemLabel="Selecione o Filtro" itemValue="" />  
                                    <f:selectItem itemLabel="Nº" itemValue="c.esc" />  
                                    <f:selectItem itemLabel="Livro" itemValue="c.book" />  
                                    <f:selectItem itemLabel="Data" itemValue="c.dateAct" />  
                                    <f:selectItem itemLabel="Partes Crescente" itemValue="peo.name" />  
                                    <f:selectItem itemLabel="Partes Decrescente" itemValue="peo.name" /> 

                                    <p:ajax update="cepSearchTable" listener="#{managerSearchCep.handleSearch}" /> 
                                </p:selectOneMenu> 
                            </div>
                            <p:dataTable var="cht" id="cepSearchTable" value="#{managerSearchCep.charters}"  
                                         style="white-space: pre-wrap;" selectionMode="single" rowKey="#{cht.id}"
                                         emptyMessage="Nenhum registro encontrado" sortMode="multiple"
                                         paginatorPosition="bottom" paginator="true" rows="5"
                                         >
                                <f:facet name="header">
                                    Escritura
                                </f:facet> 
                                <p:column headerText="Ação" style="width: 50px; vertical-align: middle;">  
                                    <p:button icon="ui-icon-search" href="showCep.xhtml?cepShow=#{cht.id}" style="width: 25px;"/>
                                    <p:button icon="ui-icon-pencil" href="editCep.xhtml?cepEdit=#{cht.id}" style="width: 25px;"/>
                                    <p:commandButton icon="ui-icon-trash" onclick="dlg3.show()"  style="width: 25px;"/>

                                </p:column> 
                                <p:column sortBy="#{cht.esc}"  headerText="Nº" >  
                                    <h:outputLabel value="#{cht.esc eq null ? '-':cht.esc}" />  
                                </p:column>
                                <p:column sortBy="#{cht.book}"  headerText="Livro" >  
                                    <h:outputLabel value="#{cht.book eq null ? '-':cht.book}" />  
                                </p:column>
                                <p:column sortBy="#{cht.actCep}" headerText="Tipo" >  
                                    <h:outputLabel value="#{cht.actCep.description.concat(' - ')
                                                            .concat(cht.natureWriting.description)}" />  
                                </p:column> 
                                <p:column sortBy="#{cht.dateAct}"  headerText="Data" width="100" >  
                                    <h:outputLabel value="#{cht.dateAct}" >
                                        <f:convertDateTime pattern="dd/MM/yyyy"/>
                                    </h:outputLabel>
                                </p:column> 
                                <p:column  headerText="Partes" >  
                                    <p:dataTable var="part" id="cepSearchTable"  emptyMessage="Nenhuma parte encontrada" value="#{cht.parts}">
                                        <p:column headerText="Qualidade" width="20">  
                                            <h:outputLabel value="#{part.partQuality eq null ? '-': part.partQuality.description}" />
                                        </p:column> 
                                        <p:column headerText="Nome">  
                                            <h:outputLabel value="#{part.people.name}" />
                                        </p:column> 
                                        <p:column headerText="Doc">  
                                            <h:outputLabel value="#{part.people.cpf ne null ? part.people.cpf:''}" />
                                            <h:outputLabel value="#{part.people.cnpj ne null ? part.people.cnpj:''}" />
                                        </p:column> 
                                    </p:dataTable>
                                </p:column> 
                            </p:dataTable>
                        </p:panel>
                    </div>
                </div>

                <p:dialog header="Exclusão de Registro" widgetVar="dlg3" modal="true"
                          showEffect="fade" hideEffect="fade" resizable="false" height="70">  
                    <h:outputText value="Deseja Excluir esse Registro ?" /> <br/> 
                    <f:facet name="footer">
                        <center>
                            <p:commandButton value="Sim" icon="ui-icon-check"
                                             actionListener="#{managerSearchCep.remove(cht)}" 
                                             update="@form"  />
                            <p:commandButton value="Não" icon="ui-icon-close"
                                             onclick="dlg3.hide();"  />
                        </center>
                    </f:facet>

                </p:dialog>

                <p:dialog id="dlgActCepDialog"  widgetVar="dlgActCepDialog"
                          header="Selecionar Tipo de Ato" modal="true"
                          width="800"  dynamic="true" closable="false" > 

                    <p:panel id="actCepPanelSearch" style="background: none; border: none;" 
                             styleClass="container_24 clearfix ">

                        <p:dataTable id="tableActCep" var="p" value="#{managerSearchCep.actCeps}" 
                                     styleClass="grid_24"
                                     rowIndexVar="rowid"
                                     rowKey="#{p.id}"
                                     rows="5" paginator="true"  
                                     paginatorPosition="bottom"
                                     selectionMode="single"
                                     selection="#{managerSearchCep.actCep}"
                                     emptyMessage="Nenhum registro">  
                            <f:facet name="header">
                                Tipo de Ato
                            </f:facet>
                            <p:ajax event="rowSelect"  process="@this" update="@this"/>
                            <p:ajax event="rowUnselect"  process="@this" update="@this"/>
                            <p:column headerText="Cod">  
                                <h:outputText value="#{p.cod}"/>
                            </p:column>  
                            <p:column headerText="Nome">  
                                <h:outputText value="#{p.description}"/>
                            </p:column>
                        </p:dataTable> 

                        <div class="buttonAction">
                            <p:commandButton value="Selecionar" icon="ui-icon-search"
                                             update=":cepForm:charterPanel" 
                                             process=":cepForm:charterPanel" 
                                             immediate="false" oncomplete="dlgActCepDialog.hide();" />

                            <p:commandButton value="Remover" icon="ui-icon-trash"
                                             update="@this, :cepForm:charterPanel" 
                                             process="@this, :cepForm:charterPanel" 
                                             actionListener="#{managerSearchCep.initActCep()}"
                                             oncomplete="dlgActCepDialog.hide();"/>
                        </div>
                    </p:panel>
                </p:dialog>

                <p:dialog id="dlgNatureDialog"  widgetVar="dlgNatureDialog"
                          header="Selecionar Natureza do Ato" modal="true"
                          width="800"  dynamic="true" closable="false" > 

                    <p:panel id="naturePanelSearch" style="background: none; border: none;" 
                             styleClass="container_24 clearfix ">

                        <p:dataTable id="tableNature" var="n" value="#{managerSearchCep.natureWritings}" 
                                     styleClass="grid_24"
                                     rowIndexVar="rowid"
                                     rowKey="#{n.id}"
                                     rows="5" paginator="true"  
                                     paginatorPosition="bottom"
                                     selectionMode="single"
                                     selection="#{managerSearchCep.natureWritingAux}"
                                     emptyMessage="Nenhum registro">  
                            <f:facet name="header">
                                Natureza do Ato
                            </f:facet>
                            <p:ajax event="rowSelect"  process="@this" update="@this"/>
                            <p:ajax event="rowUnselect"  process="@this" update="@this"/>
                            <p:column headerText="Cod">  
                                <h:outputText value="#{n.cod}"/>
                            </p:column>  
                            <p:column headerText="Nome">  
                                <h:outputText value="#{n.description}"/>
                            </p:column>
                        </p:dataTable> 

                        <div class="buttonAction">
                            <p:commandButton value="Selecionar" icon="ui-icon-search"
                                             update=":cepForm:charterPanel" 
                                             process=":cepForm:charterPanel" 
                                             oncomplete="dlgNatureDialog.hide();" />

                            <p:commandButton value="Remover" icon="ui-icon-trash"
                                             update="@this,  :cepForm:charterPanel" 
                                             process="@this,  :cepForm:charterPanel" 
                                             actionListener="#{managerSearchCep.initNature()}"
                                             oncomplete="dlgNatureDialog.hide();"/>
                        </div>
                    </p:panel>
                </p:dialog>
            </h:form>
        </ui:define>
    </ui:composition>

</html>